package com.xthink.web.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xthink.common.pojo.iPackage.CustomPackageHistory;
import com.xthink.common.pojo.iPackage.CustomSpecsHistory;
import com.xthink.common.pojo.iPackage.Package;
import com.xthink.common.pojo.iPackage.PackageSpecs;
import com.xthink.common.vo.cards.BinDingPackageListVo;
import com.xthink.common.vo.iPackage.CustomPackagePageVo;
import com.xthink.common.vo.iPackage.LikeCustomPackage;
import com.xthink.common.vo.iPackage.NewCustomPackageVo;
import com.xthink.common.vo.iPackage.PackageSpecsVo;
import com.xthink.common.constant.HttpStatus;
import com.xthink.common.result.AjaxResult;
import com.xthink.web.mapper.CustomPackageHistoryMapper;
import com.xthink.web.mapper.CustomSpecsHistoryMapper;
import com.xthink.web.mapper.PackageMapper;
import com.xthink.web.mapper.PackageSpecsMapper;
import com.xthink.web.service.PackageService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;


@Service
public class PackageServiceImpl extends ServiceImpl<PackageMapper, Package> implements PackageService {

    @Autowired
    private PackageMapper packageMapper;

    @Autowired
    private PackageSpecsMapper packageSpecsMapper;

    @Autowired
    private CustomPackageHistoryMapper customPackageHistoryMapper;

    @Autowired
    private CustomSpecsHistoryMapper customSpecsHistoryMapper;

    //新建自定义套餐
    @Override
    public AjaxResult newCustomPackage(NewCustomPackageVo newCustomPackageVo) {
        if (!StringUtils.isNotBlank(newCustomPackageVo.getPackageName())) {
            return AjaxResult.error("请输入套餐名称");
        }

        if (!StringUtils.isNotBlank(newCustomPackageVo.getSalesName())) {
            return AjaxResult.error("请输入套餐售卖名称");
        }

        if (newCustomPackageVo.getPrice() == null) {
            return AjaxResult.error("请输入套餐售卖单价");
        }

        if (newCustomPackageVo.getPackageSize() == null) {
            return AjaxResult.error("请输入套餐大小");
        }

        if (newCustomPackageVo.getMonth() == null) {
            return AjaxResult.error("请输入套餐规格");
        }

        Package aPackage = new Package();
        BeanUtils.copyProperties(newCustomPackageVo, aPackage);
        aPackage.setPackageClassification(0);

        int a = packageMapper.insert(aPackage);


        //存入销售规格表
        Integer[] month = newCustomPackageVo.getMonth();
        Double[] monthPrice = newCustomPackageVo.getMonthPrice();
        for (int i = 0; i < month.length; i++) {
            PackageSpecs packageSpecs = new PackageSpecs();
            packageSpecs.setPackageId(aPackage.getId());
            packageSpecs.setMonth(month[i]);
            packageSpecs.setMonthPrice(monthPrice[i]);
            packageSpecs.setStatus(newCustomPackageVo.getStatus());
            packageSpecsMapper.insert(packageSpecs);

            //存入销售规格历史价格表
            Integer id = packageSpecs.getId();
            CustomSpecsHistory customSpecsHistory = new CustomSpecsHistory();
            customSpecsHistory.setSpecsId(id);
            customSpecsHistory.setMonth(month[i]);
            customSpecsHistory.setMonthPrice(monthPrice[i]);
            customSpecsHistoryMapper.insert(customSpecsHistory);
        }

        //存入自定义套餐历史单价表
        CustomPackageHistory customPackageHistory = new CustomPackageHistory();
        customPackageHistory.setPackageId(aPackage.getId());
        customPackageHistory.setPackageName(aPackage.getPackageName());
        customPackageHistory.setSalesName(aPackage.getSalesName());
        customPackageHistory.setPackageType(aPackage.getPackageType());
        customPackageHistory.setPackageSize(aPackage.getPackageSize());
        customPackageHistory.setPrice(aPackage.getPrice());
        customPackageHistoryMapper.insert(customPackageHistory);


        if (a == 0) {
            return AjaxResult.error("新建套餐失败");
        }

        return AjaxResult.success();
    }

    //自定义套餐分页查询
    @Override
    public AjaxResult customPackagePage(CustomPackagePageVo customPackagePageVo) {
        Page<Package> page = new Page<>(customPackagePageVo.getPage(), customPackagePageVo.getSize());
        LambdaQueryWrapper<Package> wrapper = new LambdaQueryWrapper<>();
        //显示自定义套餐
        wrapper.eq(Package::getPackageClassification, 0);
        //显示未删除的套餐
        wrapper.eq(Package::getDelFlag, 0);
        Page<Package> packagePage = packageMapper.selectPage(page, wrapper);
        List<Package> packages = packagePage.getRecords();

        ArrayList<PackageSpecsVo> dtos = new ArrayList<>();

        for (int i = 0; i < packages.size(); i++) {
            Package aPackage = packages.get(i);
            PackageSpecsVo packageSpecsVo = new PackageSpecsVo();
            BeanUtils.copyProperties(aPackage, packageSpecsVo);
            Integer id = packageSpecsVo.getId();
            ArrayList<String> packageSpecs = this.getPackageSpecs(id);
            packageSpecsVo.setPackageSpecs(packageSpecs);
            dtos.add(packageSpecsVo);
        }
        return AjaxResult.success("查询成功", dtos);
    }

    public ArrayList<String> getPackageSpecs(Integer id) {
        LambdaQueryWrapper<PackageSpecs> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(PackageSpecs::getPackageId, id);
        List<PackageSpecs> packageSpecs = packageSpecsMapper.selectList(wrapper);

        ArrayList<String> arr = new ArrayList<>();
        for (int i = 0; i < packageSpecs.size(); i++) {
            Integer month = packageSpecs.get(i).getMonth();
            Double monthPrice = packageSpecs.get(i).getMonthPrice();
            arr.add(month + "/" + monthPrice);
        }
        return arr;
    }

    //自定义套餐上架
    @Override
    public AjaxResult customPackagePutOn(Integer[] ids) {
        for (Integer id : ids) {
            LambdaUpdateWrapper<Package> wrapper = new LambdaUpdateWrapper<>();
            wrapper.eq(Package::getId, id)
                    .set(Package::getStatus, 1);
            int total = packageMapper.update(null, wrapper);
            if (total == 0) {
                return AjaxResult.error("ID:" + id + "上架失败,该状态已是上架状态");
            }
        }
        return AjaxResult.success();
    }

    //自定义套餐下架
    @Override
    public AjaxResult customPackageOff(Integer[] ids) {
        for (Integer id : ids) {
            LambdaUpdateWrapper<Package> wrapper = new LambdaUpdateWrapper<>();
            wrapper.eq(Package::getId, id)

                    .set(Package::getStatus, 0);
            int total = packageMapper.update(null, wrapper);
            if (total == 0) {
                return AjaxResult.error("ID:" + id + "上架失败,该状态已是上架状态");
            }
        }
        return AjaxResult.success();
    }

    //自定义套餐删除
    @Override
    public AjaxResult customPackageDelete(Integer[] ids) {
        for (Integer id : ids) {
            LambdaUpdateWrapper<Package> wrapper = new LambdaUpdateWrapper<>();
            wrapper.eq(Package::getId, id).set(Package::getDelFlag, 1);
            int update = packageMapper.update(null, wrapper);
            if (update == 0) {
                return AjaxResult.error("ID:" + id + "删除失败");
            }
        }
        return AjaxResult.success();
    }

    //自定义套餐模糊搜索
    @Override
    public AjaxResult customPackageLike(LikeCustomPackage likeCustomPackage) {
        //模糊查询  条件封装
        LambdaQueryWrapper<Package> wrapper = new LambdaQueryWrapper<>();
        Page<Package> page = new Page<>(likeCustomPackage.getPage(), likeCustomPackage.getSize());
        //套餐名称
        wrapper.like(StringUtils.isNotBlank(likeCustomPackage.getPackageName()), Package::getPackageName, likeCustomPackage.getPackageName());
        wrapper.eq(likeCustomPackage.getPackageType() != null, Package::getPackageType, likeCustomPackage.getPackageType());
        wrapper.eq(likeCustomPackage.getStatus() != null, Package::getStatus, likeCustomPackage.getStatus());
        wrapper.eq(Package::getPackageClassification, 0);

        if (!StringUtils.isNotBlank(likeCustomPackage.getPackageName()) && likeCustomPackage.getPackageType() == null && likeCustomPackage.getStatus() == null) {
            return AjaxResult.error("搜索失败", HttpStatus.BAD_REQUEST);
        }

        Page<Package> packagePage = packageMapper.selectPage(page, wrapper);
        List<Package> packages = packagePage.getRecords();

        if (packages.size() == 0) {
            return AjaxResult.error("未搜索到此套餐");
        }

        return AjaxResult.success("查询成功", packages);
    }

    //自定义套餐详情
    @Override
    public AjaxResult customPackageDetails(Integer id) {

        Package aPackage = packageMapper.selectById(id);

        return AjaxResult.success(aPackage);
    }

    //修改自定义套餐单价
    @Override
    public AjaxResult customPackageUpdatePrice(Integer id, Double price) {

        UpdateWrapper<Package> wrapper = new UpdateWrapper<>();
        wrapper.eq("id", id).set("price", price);
        int update = packageMapper.update(null, wrapper);

        if (update == 0) {
            return AjaxResult.error("修改价格失败");
        }

        //添加到自定义套餐历史单价表
        LambdaQueryWrapper<Package> QueryWrapper = new LambdaQueryWrapper<>();
        QueryWrapper.eq(Package::getId, id);
        Package aPackage = packageMapper.selectOne(QueryWrapper);

        CustomPackageHistory customPackageHistory = new CustomPackageHistory();
        customPackageHistory.setPackageId(id);
        customPackageHistory.setPackageName(aPackage.getPackageName());
        customPackageHistory.setSalesName(aPackage.getSalesName());
        customPackageHistory.setPackageType(aPackage.getPackageType());
        customPackageHistory.setPackageSize(aPackage.getPackageSize());
        customPackageHistory.setPrice(aPackage.getPrice());
        customPackageHistoryMapper.insert(customPackageHistory);
        return AjaxResult.success();
    }


    //新建标准套餐
    @Override
    public AjaxResult newPipPackage(NewCustomPackageVo newCustomPackageVo) {
        if (!StringUtils.isNotBlank(newCustomPackageVo.getPackageName())) {
            return AjaxResult.error("请输入套餐名称");
        }

        if (!StringUtils.isNotBlank(newCustomPackageVo.getSalesName())) {
            return AjaxResult.error("请输入套餐售卖名称");
        }

        if (newCustomPackageVo.getPrice() == null) {
            return AjaxResult.error("请输入套餐售卖单价");
        }

        if (newCustomPackageVo.getPackageSize() == null) {
            return AjaxResult.error("请输入套餐大小");
        }

        if (newCustomPackageVo.getMonth() == null) {
            return AjaxResult.error("请输入套餐规格");
        }

        Package aPackage = new Package();
        aPackage.setPackageClassification(1);
        BeanUtils.copyProperties(newCustomPackageVo, aPackage);

        int a = packageMapper.insert(aPackage);


        //存入销售规格表
        Integer[] month = newCustomPackageVo.getMonth();
        Double[] monthPrice = newCustomPackageVo.getMonthPrice();
        for (int i = 0; i < month.length; i++) {
            PackageSpecs packageSpecs = new PackageSpecs();
            packageSpecs.setPackageId(aPackage.getId());
            packageSpecs.setMonth(month[i]);
            packageSpecs.setMonthPrice(monthPrice[i]);
            packageSpecs.setStatus(newCustomPackageVo.getStatus());
            packageSpecsMapper.insert(packageSpecs);

            //存入销售规格历史价格表
            Integer id = packageSpecs.getId();
            CustomSpecsHistory customSpecsHistory = new CustomSpecsHistory();
            customSpecsHistory.setSpecsId(id);
            customSpecsHistory.setMonth(month[i]);
            customSpecsHistory.setMonthPrice(monthPrice[i]);
            customSpecsHistoryMapper.insert(customSpecsHistory);
        }

        //存入自定义套餐历史单价表
        CustomPackageHistory customPackageHistory = new CustomPackageHistory();
        customPackageHistory.setPackageId(aPackage.getId());
        customPackageHistory.setPackageName(aPackage.getPackageName());
        customPackageHistory.setSalesName(aPackage.getSalesName());
        customPackageHistory.setPackageType(aPackage.getPackageType());
        customPackageHistory.setPackageSize(aPackage.getPackageSize());
        customPackageHistory.setPrice(aPackage.getPrice());
        customPackageHistoryMapper.insert(customPackageHistory);


        if (a == 0) {
            return AjaxResult.error("新建套餐失败");
        }

        return AjaxResult.success();
    }

    //标准套餐分页查询
    @Override
    public AjaxResult pipPackagePage(CustomPackagePageVo customPackagePageVo) {
        Page<Package> page = new Page<>(customPackagePageVo.getPage(), customPackagePageVo.getSize());
        LambdaQueryWrapper<Package> wrapper = new LambdaQueryWrapper<>();
        //显示标准套餐
        wrapper.eq(Package::getPackageClassification, 1);
        //显示未删除的套餐
        wrapper.eq(Package::getDelFlag, 0);
        Page<Package> packagePage = packageMapper.selectPage(page, wrapper);
        List<Package> packages = packagePage.getRecords();

        ArrayList<PackageSpecsVo> dtos = new ArrayList<>();

        for (int i = 0; i < packages.size(); i++) {
            Package aPackage = packages.get(i);
            PackageSpecsVo packageSpecsVo = new PackageSpecsVo();
            BeanUtils.copyProperties(aPackage, packageSpecsVo);
            Integer id = packageSpecsVo.getId();
            ArrayList<String> packageSpecs = this.getPackageSpecs(id);
            packageSpecsVo.setPackageSpecs(packageSpecs);
            dtos.add(packageSpecsVo);
        }
        return AjaxResult.success("查询成功", dtos);
    }

    //标准套餐上架
    @Override
    public AjaxResult pipPackagePutOn(Integer[] ids) {
        for (int i = 0; i < ids.length; i++) {
            UpdateWrapper<Package> wrapper = new UpdateWrapper<>();
            wrapper.eq("id", ids[i])
                    .set("status", 1);
            int total = packageMapper.update(null, wrapper);
            if (total == 0) {
                return AjaxResult.error("ID:" + ids[i] + "上架失败,该状态已是上架状态");
            }
        }
        return AjaxResult.success();
    }

    //标准套餐下架
    @Override
    public AjaxResult pipPackageOff(Integer[] ids) {
        for (Integer id : ids) {
            UpdateWrapper<Package> wrapper = new UpdateWrapper<>();
            wrapper.eq("id", id)
                    .set("status", 0);
            int total = packageMapper.update(null, wrapper);
            if (total == 0) {
                return AjaxResult.error("ID:" + id + "下架失败,该状态已是下架状态");
            }
        }
        return AjaxResult.success();
    }

    //标准套餐删除
    @Override
    public AjaxResult pipPackageDelete(Integer[] ids) {
        for (Integer id : ids) {
            LambdaUpdateWrapper<Package> wrapper = new LambdaUpdateWrapper<>();
            wrapper.eq(Package::getId, id).set(Package::getDelFlag, 1);
            int update = packageMapper.update(null, wrapper);
            if (update == 0) {
                return AjaxResult.error("ID:" + id + "删除失败");
            }
        }
        return AjaxResult.success();
    }

    //标准套餐模糊搜索
    @Override
    public AjaxResult pipPackageLike(LikeCustomPackage likeCustomPackage) {
        //模糊查询  条件封装
        LambdaQueryWrapper<Package> wrapper = new LambdaQueryWrapper<>();
        Page<Package> page = new Page<>(likeCustomPackage.getPage(), likeCustomPackage.getSize());
        //套餐名称
        wrapper.like(StringUtils.isNotBlank(likeCustomPackage.getPackageName()), Package::getPackageName, likeCustomPackage.getPackageName());
        wrapper.eq(likeCustomPackage.getPackageType() != null, Package::getPackageType, likeCustomPackage.getPackageType());
        wrapper.eq(likeCustomPackage.getStatus() != null, Package::getStatus, likeCustomPackage.getStatus());
        wrapper.eq(Package::getPackageClassification, 1);

        if (!StringUtils.isNotBlank(likeCustomPackage.getPackageName()) && likeCustomPackage.getPackageType() == null && likeCustomPackage.getStatus() == null) {
            return AjaxResult.error("搜索失败", HttpStatus.BAD_REQUEST);
        }

        Page<Package> packagePage = packageMapper.selectPage(page, wrapper);
        List<Package> packages = packagePage.getRecords();

        if (packages.size() == 0) {
            return AjaxResult.error("未搜索到此套餐");
        }

        return AjaxResult.success("查询成功", packages);
    }

    //标准套餐详情
    @Override
    public AjaxResult pipPackageDetails(Integer id) {

        Package aPackage = packageMapper.selectById(id);

        return AjaxResult.success(aPackage);
    }

    //修改标准套餐单价
    @Override
    public AjaxResult pipPackageUpdatePrice(Integer id, Double price) {
        UpdateWrapper<Package> wrapper = new UpdateWrapper<>();
        wrapper.eq("id", id).set("price", price);
        int update = packageMapper.update(null, wrapper);

        if (update == 0) {
            return AjaxResult.error("修改价格失败");
        }

        //添加到自定义套餐历史单价表
        LambdaQueryWrapper<Package> QueryWrapper = new LambdaQueryWrapper<>();
        QueryWrapper.eq(Package::getId, id);
        Package aPackage = packageMapper.selectOne(QueryWrapper);

        CustomPackageHistory customPackageHistory = new CustomPackageHistory();
        customPackageHistory.setPackageId(id);
        customPackageHistory.setPackageName(aPackage.getPackageName());
        customPackageHistory.setSalesName(aPackage.getSalesName());
        customPackageHistory.setPackageType(aPackage.getPackageType());
        customPackageHistory.setPackageSize(aPackage.getPackageSize());
        customPackageHistory.setPrice(aPackage.getPrice());
        customPackageHistoryMapper.insert(customPackageHistory);
        return AjaxResult.success();
    }

    //卡片绑定套餐中的套餐详情
    @Override
    public AjaxResult packageDetails(Integer id) {

        Package aPackage = packageMapper.selectById(id);

        return AjaxResult.success(aPackage);
    }

    //卡片管理绑定套餐的模糊搜索和分页查询
    @Override
    public AjaxResult binDingPackageList(BinDingPackageListVo binDingPackageListVo) {

        Page<Package> page = new Page<>(binDingPackageListVo.getPage(), binDingPackageListVo.getSize());
        LambdaQueryWrapper<Package> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(
                StringUtils.isNotBlank(binDingPackageListVo.getPackageName())
                ,Package::getPackageName
                ,binDingPackageListVo.getPackageName()
                );

        wrapper.eq(
                binDingPackageListVo.getPackageClassification()!=null
                ,Package::getPackageClassification
                ,binDingPackageListVo.getPackageClassification()
                );

        wrapper.eq(
                binDingPackageListVo.getPackageType()!=null
                ,Package::getPackageType
                ,binDingPackageListVo.getPackageType()
        );

        Page<Package> page1 = packageMapper.selectPage(page, wrapper);
        List<Package> records = page1.getRecords();
        if (records.size()==0){
            return AjaxResult.error("未查询到此套餐");
        }
        return AjaxResult.success(records);
    }

}
